package RegisterServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/regist") // 与前端 form action 路径一致
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 解决中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 2. 获取表单数据
        String stuid = request.getParameter("stuid");
        String password = request.getParameter("password");
        String confirmPwd = request.getParameter("confirmPwd");
        String name = request.getParameter("name");
        String grade = request.getParameter("grade");
        String clazz = request.getParameter("clazz");

        // 3. 模拟业务逻辑（这里简单打印到控制台，实际可连接数据库入库）
        System.out.println("=== 注册信息 ===");
        System.out.println("学号：" + stuid);
        System.out.println("姓名：" + name);
        System.out.println("年级：" + grade);
        System.out.println("班级：" + clazz);
        // ... 其他字段可按需处理

        // 4. 回显数据到浏览器（可通过 HTML 或 JavaScript 展示，这里用简单方式）
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h3>注册请求已接收，信息如下：</h3>");
        out.println("学号：" + stuid + "<br>");
        out.println("姓名：" + name + "<br>");
        out.println("年级：" + grade + "<br>");
        out.println("班级：" + clazz + "<br>");
        out.println("<a href='index.html'>返回注册页</a>");
        out.println("</body></html>");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // GET 请求直接转发到注册页（简单处理，实际可优化）
        request.getRequestDispatcher("index.html").forward(request, response);
    }
}